package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PartLot;
import com.ruoyi.system.mapper.PartPostMapper;
import com.ruoyi.system.domain.PartPost;
import com.ruoyi.system.service.IPartPostService;
import com.ruoyi.common.core.text.Convert;

/**
 * partpostService业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-06
 */
@Service
public class PartPostServiceImpl implements IPartPostService 
{
    @Autowired
    private PartPostMapper partPostMapper;

    /**
     * 查询partpost
     * 
     * @param partPostId partpost主键
     * @return partpost
     */
    @Override
    @Cacheable(value = "sys-aaaaaaaaaa",key = "#partPostId")
    public PartPost selectPartPostByPartPostId(Long partPostId)
    {
        return partPostMapper.selectPartPostByPartPostId(partPostId);
    }

    /**
     * 查询partpost列表
     * 
     * @param partPost partpost
     * @return partpost
     */
    @Override
    public List<PartPost> selectPartPostList(PartPost partPost)
    {
        return partPostMapper.selectPartPostList(partPost);
    }

    /**
     * 新增partpost
     * 
     * @param partPost partpost
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPartPost(PartPost partPost)
    {
        partPost.setCreateTime(DateUtils.getNowDate());
        int rows = partPostMapper.insertPartPost(partPost);
        insertPartLot(partPost);
        return rows;
    }

    /**
     * 修改partpost
     * 
     * @param partPost partpost
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePartPost(PartPost partPost)
    {
        partPost.setUpdateTime(DateUtils.getNowDate());
        partPostMapper.deletePartLotByPartLotId(partPost.getPartPostId());
        insertPartLot(partPost);
        return partPostMapper.updatePartPost(partPost);
    }

    /**
     * 批量删除partpost
     * 
     * @param partPostIds 需要删除的partpost主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePartPostByPartPostIds(String partPostIds)
    {
        partPostMapper.deletePartLotByPartLotIds(Convert.toStrArray(partPostIds));
        return partPostMapper.deletePartPostByPartPostIds(Convert.toStrArray(partPostIds));
    }

    /**
     * 删除partpost信息
     * 
     * @param partPostId partpost主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePartPostByPartPostId(Long partPostId)
    {
        partPostMapper.deletePartLotByPartLotId(partPostId);
        return partPostMapper.deletePartPostByPartPostId(partPostId);
    }

    /**
     * 新增停车场信息信息
     * 
     * @param partPost partpost对象
     */
    public void insertPartLot(PartPost partPost)
    {
        List<PartLot> partLotList = partPost.getPartLotList();
        Long partPostId = partPost.getPartPostId();
        if (StringUtils.isNotNull(partLotList))
        {
            List<PartLot> list = new ArrayList<PartLot>();
            for (PartLot partLot : partLotList)
            {
                partLot.setPartLotId(partPostId);
                list.add(partLot);
            }
            if (list.size() > 0)
            {
                partPostMapper.batchPartLot(list);
            }
        }
    }
}
